package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
import ModelLayer.*;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 

    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }

    public void start()
    {
        dvdMenu();
    }

    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if (choice == 1)
            {
                findDVD();
            }
            else
            {
                if(choice == 2)
                {
                    createDVD();
                }
                else
                {
                    if(choice == 3)
                    {
                        createCopy();
                    }
                    else
                    {
                        if(choice == 4)
                        {
                            deleteDVD();
                        }
                        else
                        {
                            if(choice == 5)
                            {
                                updateDVD();
                            }
                            else
                            {
                                if (choice == 6)
                                {
                                    listAllDvds();
                                }
                                else
                                {
                                    exit = true;
                                }
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else

        }//end while
    }

    private int writeDVDMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD");
        System.out.println(" (3) Create Copy");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Update DVD");
        System.out.println(" (6) Show current DVD list");
        System.out.println(" (7) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD number: ");
        int no = keyboard.nextInt();
        return no;
    }

    private int inputAmount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the Amount: ");
        int amount = keyboard.nextInt();
        return amount;
    }

    private String inputReleasedate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the Releasedate: ");
        String releasedate = keyboard.nextLine();
        return releasedate;
    }

    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }

    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the number: ");
        int no = keyboard.nextInt();
        return no;
    }

    private String inputSerialNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the serial number: ");
        String serialno = keyboard.nextLine();
        return serialno;
    }

    private String inputPurchaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the purchase date: ");
        String purchasedate = keyboard.nextLine();
        return purchasedate;
    }

    private String inputPurchasePrice()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the purchase date: ");
        String purchaseprice = keyboard.nextLine();
        return purchaseprice;
    }

    //there is need for more methods to read information about DVDs
    //and methods to display the information

    private void findDVD()
    {   
        //System.out.println(dvdCtr.findDVD(inputTitle()).toString());
        String title = inputTitle();
        dvdCtr.findDVD(title).printInfo();
        pause();
    }

    private void createDVD()
    {
        //int no = inputCopyNo();
        //if(dvdCtr.findDVD(no) == null)
        //{
        //    String title = inputTitle();
        //    String releasedate = inputReleasedate();
        //    dvdCtr.createDVD(no, title, releasedate);
        //}
        //else
        //{
        //    System.out.println("Already exists");
        //    pause();
        //}
        dvdCtr.createDVD(inputDVDNo(), inputTitle(), inputReleasedate());
    }

    private void createCopy()
    {
        String t = inputTitle();
        dvdCtr.addCopy(t, inputSerialNo(), inputPurchaseDate(), inputPurchasePrice());
        pause();

    }

    private void deleteDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the title of the DVD: " );
        String title = keyboard.nextLine();
        dvdCtr.deleteDVD(title);
    }

    private void listAllDvds()
    {
        dvdCtr.listAllDVDs();
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter to continue");
        String address = keyboard.nextLine();
    }

    //private void updateDVD()
    //{
    //    System.out.println('\f'+"Enter the title of the DVD you want to update");
    //    String searchTitle = inputTitle();
    //   dvdCtr.updateDVD(t, t, inputReleaseDate());
    //}

    private void updateDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the title of the DVD: " );
        String title = keyboard.nextLine();
        if (dvdCtr.findDVD(title)!= null)
        {
            DVD d = dvdCtr.findDVD(title);
            updateDvdMenu(d);
        }
    }

    public void updateDvdMenu(DVD d)
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = updateDVDMenu(); 
            if(choice == 1)
            {   
                updateSerialNo(d);
            }
            else
            {
                if(choice == 2)
                {
                    updateTitle(d);
                }  
                else
                {
                    if(choice == 3)
                    {
                        updateReleaseDate(d);
                    }  
                    else
                    {
                        exit = true;
                    }//end else
                }//end else
            }//end else
        }//end else
    }//end else
    
    public int updateDVDMenu()
    {    
          Scanner keyboard = new Scanner(System.in);
          System.out.println("\f *** Update DVD Menu ***");
          System.out.println(" (1) Update Serial Number");
          System.out.println(" (2) Update Title");
          System.out.println(" (3) Update Release Date");
          System.out.println(" (4) Return");
          System.out.print("\n Make your choice: ");
            
          int choice = keyboard.nextInt();
          return choice;
    }
    
    public void updateSerialNo(DVD d)
    {
        showNo(d.getNo());
        d.setNo(inputDVDNo());
    }
    
    public void updateTitle(DVD d)
    {
        showTitle(d.getTitle());
        d.setTitle(inputTitle());
    }
    
    public void updateReleaseDate(DVD d)
    {
        showReleaseDate(d.getReleasedate());
        d.setReleasedate(inputReleasedate());
    }
    
    public void showNo(int no)
    {
        System.out.println("Serial number of the DVD: " + no);
    }
    
    public void showTitle(String title)
    { 
        System.out.println("Title of the DVD: " + title);
    }
    
    public void showReleaseDate(String releaseDate)
    { 
        System.out.println("Release Date of the DVD: " + releaseDate);
    }
}